//
//  RHPackCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/1/8.
//

#import "RHPackCell.h"
#import "RHPackItemCell.h"

@interface RHPackCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
//下面的电芯
@property (nonatomic ,strong) UICollectionView *packCollectionView;

@property (nonatomic ,strong) UIView *lineView;

@property (nonatomic ,strong) CAShapeLayer *lineLayer;
@end
@implementation RHPackCell
-(instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self setupUI];
    }
    return self;
}

-(void)setPackModel:(RHDevPackModel *)packModel
{
    _packModel = packModel;
    self.packView.packModel = packModel;
    [self updateLineLayer];
    [self.packCollectionView reloadData];
    self.lineLayer.hidden = packModel.cells.count == 0;
}


-(void)updateLineLayer
{
    UIBezierPath *path = [UIBezierPath bezierPath];
    //cell的宽度
    CGFloat w = (kUI_WINDOW_W-kRightOffset-15-20-20)/2;
    CGFloat lineWidth = kUI_WINDOW_W - 15 - kRightOffset;
    //中间竖线 上
    UIBezierPath *path_center_1 = [UIBezierPath bezierPath];
    [path_center_1 moveToPoint:CGPointMake(lineWidth/2, 0)];
    [path_center_1 addLineToPoint:CGPointMake(lineWidth/2, self.lineView.height*0.7)];
    
    
    
    //横线 左
    UIBezierPath *path_h_left = [UIBezierPath bezierPath];
    [path_h_left moveToPoint:CGPointMake(w*0.5+5, self.lineView.height*0.7)];
    [path_h_left addLineToPoint:CGPointMake(lineWidth/2, self.lineView.height*0.7)];
 
    //横线 右
    UIBezierPath *path_h_right = [UIBezierPath bezierPath];
    [path_h_right moveToPoint:CGPointMake(lineWidth/2, self.lineView.height*0.7)];
    [path_h_right addLineToPoint:CGPointMake(lineWidth-w*0.5, self.lineView.height*0.7)];
    
    //左边 小竖线
    UIBezierPath *path_v_left = [UIBezierPath bezierPath];
    [path_v_left moveToPoint:CGPointMake(w*0.5+5, self.lineView.height*0.7)];
    [path_v_left addLineToPoint:CGPointMake(w*0.5+5, self.lineView.height)];
 
    // 右边 小竖线
    UIBezierPath *path_v_right = [UIBezierPath bezierPath];
    [path_v_right moveToPoint:CGPointMake(lineWidth-w*0.5, self.lineView.height*0.7)];
    [path_v_right addLineToPoint:CGPointMake(lineWidth-w*0.5, self.lineView.height)];
  
    if(self.packModel.cells.count == 1){
        [path appendPath:path_center_1];
        [path appendPath:path_h_left];
        [path appendPath:path_v_left];
    }else if(self.packModel.cells.count == 2){
        [path appendPath:path_center_1];
       
        [path appendPath:path_h_left];
        [path appendPath:path_v_left];
    }else{
        [path appendPath:path_center_1];
    
        [path appendPath:path_h_left];
        [path appendPath:path_h_right];
        [path appendPath:path_v_left];
        [path appendPath:path_v_right];
    }
    
    self.lineLayer.path = path.CGPath;
}

- (void)setupUI{
    
    kWeakSelf;
   
    UIView *bgView = kView;
    bgView.backgroundColor = CFTabViewBgColor;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    
    RHPackView *packview = [[RHPackView alloc]init];
    packview.packModel = self.packModel;
    
    [bgView addSubview:packview];
    self.packView = packview;
    [packview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_offset(5);
        make.right.mas_offset(-5);
        make.height.offset(kScaleW(110));
        
    }];
    
   
    
    //虚线
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W-15-kRightOffset,kScaleW(40) )];
    [bgView addSubview:lineView];
    self.lineView = lineView;
    
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.strokeColor = kCOLOR_HEX(@"CCCCCC").CGColor;
    layer.lineWidth = 1;
    layer.lineCap = kCALineCapSquare;
    layer.lineDashPattern = @[@(2),@(2)];
    self.lineLayer = layer;
    [lineView.layer addSublayer:layer];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(packview.mas_bottom);
        make.width.offset(kUI_WINDOW_W-15-kRightOffset);
        make.height.offset(kScaleW(40));
    }];
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _packCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    [_packCollectionView registerClass:[RHPackItemCell class] forCellWithReuseIdentifier:@"RHPackItemCell"];
    _packCollectionView.backgroundColor = CFTabViewBgColor;
    _packCollectionView.showsVerticalScrollIndicator = NO;
    _packCollectionView.showsHorizontalScrollIndicator = NO;
    _packCollectionView.delegate = self;
    _packCollectionView.dataSource = self;
    [bgView addSubview:_packCollectionView];
    
    [_packCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView.mas_bottom);
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.bottom.mas_offset(0);
    }];
      
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    return self.packModel.cells.count;
    
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    RHPackItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RHPackItemCell" forIndexPath:indexPath];
    //要先赋值温度
     cell.titleLab.text = [NSString stringWithFormat:@"%@ %ld",@"电芯".rh_localized,indexPath.item+1];
     
     RHDevCellModel *cellModel = self.packModel.cells[indexPath.item];
     cell.tempLab.text = [NSString stringWithFormat:@"%@℃",cellModel.temp];
     cell.cellModel = cellModel;
     BOOL hidden = NO;
     if(indexPath.item>=self.packModel.cells.count - 1){
         hidden = YES;
     }
    
   
    if(self.packModel.cells.count - indexPath.item <=2){
        hidden = YES;
    }
  
    [cell hideBottomLine:hidden];
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    CGFloat w = (kUI_WINDOW_W-kRightOffset-15-20-20)/2;
    return CGSizeMake(w, w*108/146+20);
    
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    return UIEdgeInsetsMake(0, 5, 0, 5);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
   
    return 0;
}
@end
